package tongcheng.com.tongcheng.base;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.luck.picture.lib.entity.LocalMedia;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import flyn.Eyes;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.utils.AppManager;

public abstract class BaseActivity extends AppCompatActivity {

    private FragmentTransaction mTransaction;
    private FragmentManager mFm;
    private SystemBarTintManager mTintManager;
    private int mCheckedBoxDrawable;
    private int previewColor;
    private int completeColor;
    private int previewBottomBgColor;
    private int bottomBgColor;
    private int themeStyle;
    public Bundle savedInstanceState;
    private List<LocalMedia> selectList;

    public SystemBarTintManager getTintManager() {
        return mTintManager;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       /* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
                ) {

        } else {
            ll_se.setVisibility(View.GONE);

        }*/

        this.savedInstanceState = savedInstanceState;
        mCheckedBoxDrawable = R.drawable.select_cb;
        previewColor = ContextCompat.getColor(this, R.color.blue);
        completeColor = ContextCompat.getColor(this, R.color.blue);
        previewBottomBgColor=ContextCompat.getColor(this, R.color.white);
        bottomBgColor=ContextCompat.getColor(this, R.color.white);
        themeStyle = ContextCompat.getColor(this, R.color.blue);

      /*  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            mTintManager = new SystemBarTintManager(this);
            // 激活状态栏
            mTintManager.setStatusBarTintEnabled(true);
            // enable navigation bar tint 激活导航栏
            mTintManager.setNavigationBarTintEnabled(false);
            //设置系统栏设置颜色
            //tintManager.setTintColor(R.color.red);
            //给状态栏设置颜色
            //mTintManager.setStatusBarTintResource(R.color.title_bar);
            //Apply the specified drawable or color resource to the system navigation bar.
            //给导航栏设置资源
            mTintManager.setNavigationBarTintResource(R.color.title_bar);
        } else {


        }
        hideBottomUIMenu();*/
        Eyes.translucentStatusBar(BaseActivity.this, true);
       /* this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }*/
        setContentView(initResid());

        //去掉虚拟键盘
//        AndroidBug5497Workaround.assistActivity(this);
        AppManager.getAppManager().addActivity(this);

        Eyes.translucentStatusBar(this, false);
//        hideBottomUIMenu();
        ButterKnife.bind(this);
        //AndroidBug54971Workaround.assistActivity(findViewById(((ViewGroup)findViewById(android.R.id.content)).getChildAt(0).getId()));
        mFm = getSupportFragmentManager();
        //开启事务
        mTransaction = mFm.beginTransaction();
        initCreate(savedInstanceState);
        Log.e("classname", getClass().getName());
    }

    public FragmentTransaction getTransaction() {

        mTransaction=mFm.beginTransaction();

        return mTransaction;
    }


    public void settitlebar(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

        } else {
            view.setPadding(0,0,0,0);
        }
    }

    //初始化
    public abstract Integer initResid();

    //初始化
    public abstract void initCreate(Bundle savedInstanceState);


    public void startactivity(Context context, Class<? extends BaseActivity> activityClass) {
        Intent intent = new Intent(context,activityClass);
        context.startActivity(intent);
    }


   /* *//**
     * 隐藏虚拟按键，并且全屏
     *//*
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);

        }
    }*/

//    @Override
//    public void onWindowFocusChanged(boolean hasFocus) {
//        super.onWindowFocusChanged(hasFocus);
//        if( hasFocus ) {
//            hideBottomUIMenu();
//        }
//    }

//    public void hideBottomUIMenu() {
//        // TODO Auto-generated method stub
//        final View decorView = getWindow().getDecorView();
//        final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
//        decorView.setSystemUiVisibility(flags);
//        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
//            @Override
//            public void onSystemUiVisibilityChange(int visibility) {
//                if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
//                    decorView.setSystemUiVisibility(flags);
//                }
//            }
//        });
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CallServer.getInstance().cancelAll();
    }

    /**
     * 检查手机上是否安装了指定的软件
     *
     * @param context
     * @param packageName：应用包名
     * @return
     */
    public boolean isAvilible(Context context, String packageName) {
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }



    public static boolean checkAliPayInstalled(Context context) {

        Uri uri = Uri.parse("alipays://platformapi/startApp");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        ComponentName componentName = intent.resolveActivity(context.getPackageManager());
        return componentName != null;
    }


    public static boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }
        return false;
    }

}




